USE [Test_Events_Temp]
GO

SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO


IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[spGetControversyIndicatorsByCompanyIds]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[spGetControversyIndicatorsByCompanyIds]
GO

CREATE PROCEDURE [dbo].[spGetControversyIndicatorsByCompanyIds]
	@CompanyIds NVARCHAR(MAX)
AS
BEGIN
	SET NOCOUNT ON;

	DECLARE @XML XML
	SET @XML = CONVERT(XML, @CompanyIds, 1)

	DECLARE @SelectedCompanies TABLE
	(
		Id uniqueidentifier
	)
	INSERT INTO @SelectedCompanies
	SELECT TID.id.value('.','uniqueidentifier') AS Id
	FROM @XML.nodes('/CompanyIds') AS T(c) outer apply T.c.nodes('Id') as TID(id)

	SELECT [CompanyId]
		  ,[ControversyIndicatorId]
		  ,[ControversyIndicatorNumber]
		  ,[AnswerCategoryText]
		  ,[AnswerCategoryScore]
	FROM [dbo].[PublishedEventIndicators] AS PublishedEventIndicators
	INNER JOIN @SelectedCompanies AS SelectedCompanies ON SelectedCompanies.Id = PublishedEventIndicators.CompanyId
	WHERE PublishedEventIndicators.ProvidesControversy = 1
END
GO


